package com.yuliang.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MyCglibProxy {
    public static void main(String[] args) {
        UserService userService = new UserService();

        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(MyCglibProxy.class.getClassLoader());
        enhancer.setSuperclass(userService.getClass());

        MethodInterceptor methodInterceptor = new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                System.out.println("hihihi");
                Object invoke = method.invoke(userService, args);
                System.out.println("bye bye bye");
                return invoke;
            }
        };
        enhancer.setCallback(methodInterceptor);

        UserService proxy = (UserService) enhancer.create();
        proxy.login("sa","666");
        proxy.register();
    }
}
